package cn.hdmis.service.impl;

import java.io.Serializable;
import java.util.Date;
import java.util.List;
import java.util.Map;
import cn.hdmis.bean.QueryResult;
import cn.hdmis.dao.GradeDao;
import cn.hdmis.dao.StudentDao;
import cn.hdmis.dao.StudentGradeDao;
import cn.hdmis.entity.Grade;
import cn.hdmis.entity.Student;
import cn.hdmis.entity.StudentGrade;
import cn.hdmis.service.StudentService;
import cn.hdmis.util.ObjectUtil;

public class StudentServiceImpl implements StudentService{
	private StudentDao studentDao;
	private GradeDao gradeDao;
	private StudentGradeDao studentGradeDao;
	
	public StudentGradeDao getStudentGradeDao() {
		return studentGradeDao;
	}

	public void setStudentGradeDao(StudentGradeDao studentGradeDao) {
		this.studentGradeDao = studentGradeDao;
	}

	public StudentDao getStudentDao() {
		return studentDao;
	}

	public void setStudentDao(StudentDao studentDao) {
		this.studentDao = studentDao;
	}

	public GradeDao getGradeDao() {
		return gradeDao;
	}

	public void setGradeDao(GradeDao gradeDao) {
		this.gradeDao = gradeDao;
	}

	@Override
	public void delStudent(Serializable id) {
		this.studentDao.delete(id);
	}

	@Override
	public int delStudents(List<Serializable> idList) {
		int i = 0;
		for (Serializable id : idList) {
			this.delStudent(id);
			i++;
		}
		return i;
	}

	@Override
	public void saveStudent(Student stu) {
		this.studentDao.save(stu);
		
	}

	@Override
	public void updateStudent(Student newStu) {
		this.studentDao.update(newStu);
	}
	
	@Override
	public Student getStudent(Long id) {
		return this.studentDao.findByID(id);
	}
	
	@Override
	public QueryResult<Student> getAllStudent() {
		return  this.studentDao.getScrollDataCriteria(0, 50, null,null);
	}
	
	@Override
	public QueryResult<Student> getAllStudent(int firstindex,int pageSize) {
		return  this.studentDao.getScrollData(firstindex, pageSize);
	}
	
	@Override
	public QueryResult<Student> getAllStudent(int firstindex, int pageSize,
			Map<String,Object> queryParamsMap) {
		Map<String,Object> paramsMap = ObjectUtil.filterMapNull(queryParamsMap);
		return this.studentDao.getScrollData(firstindex, pageSize, paramsMap);
	}
	
	@Override
	public void updateGrade(Long persionId, Long gradeID) {
		Student stu = studentDao.findByID(persionId);
		Grade grd = gradeDao.findByID(Integer.parseInt(String.valueOf(gradeID)));
		StudentGrade sg = new StudentGrade();
		sg.setStudent(stu);
		sg.setGrade(grd);
		sg.setCreatDate(new Date());
		sg.setStatus(true);
	
		try {
			this.studentGradeDao.addStudentGrade(sg);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
